package com.chenyue.cm.user.service.impl;


import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.user.dao.JdUserCollectionMapper;
import com.chenyue.cm.user.domain.JdUserCollection;
import com.chenyue.cm.user.service.JdUserCollectionService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;


/**
 * @author chenyue-cm
 */
@Service(value = "JdUserCollectionService")
public class JdUserCollectionServiceImpl extends BaseServiceImpl<JdUserCollection> implements JdUserCollectionService {

    private final JdUserCollectionMapper jdUserCollectionMapper;

    public JdUserCollectionServiceImpl(
            @Qualifier("jdUserCollectionMapper") JdUserCollectionMapper jdUserCollectionMapper
    ) {
        this.jdUserCollectionMapper = jdUserCollectionMapper;
    }

    @Override
    @Resource(name = "jdUserCollectionMapper")
    public void setBaseMapper(LcBaseMapper<JdUserCollection, Serializable> baseMapper) {
        this.baseMapper = baseMapper;
    }

    @Override
    public int getCollectCount(Integer userId) {
        return lambdaQuery().eq(JdUserCollection::getUserId, userId).count();
    }

    /**
     * 取消收藏
     *
     * @param collection 收藏内容
     */
    @Override
    public void cancelCollect(JdUserCollection collection) {
        final JdUserCollection one = lambdaQuery().eq(JdUserCollection::getColId, collection.getUserId())
                .eq(JdUserCollection::getColType, collection.getColType())
                .eq(JdUserCollection::getUserId, collection.getUserId())
                .one();
        if (one != null) {
            jdUserCollectionMapper.deleteById(one.getId());
        }
    }
}
